package com.jykj.modbus.manage;

import com.jykj.modbus.equipment.entity.CollectInformation;
import com.jykj.modbus.utils.FlowMeterUtils;
import com.jykj.modbus.utils.MathUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大连卓川科技-流量计数据处理
 */
public class WaterMeterSensor {

    public static Map<String , Object> WaterMeterMessageConversion(String hexStr , String type){
        Map<String , Object> mapData = new HashMap<>();
        List<CollectInformation> collectData = new ArrayList<CollectInformation>();
        if (type.equals("301")){
            //当前设备正累积流量
            CollectInformation collect1 = new CollectInformation();
            //01030403D200005A4E
            //010304 DFD40001 401F
            String s = hexStr.substring(6, 14);
//            String s1 = s.substring(4);
//            String s2 = s.substring(4, 8);
//            double transfer = FlowMeterUtils.transfer(s);
//            BigDecimal bg = new BigDecimal(transfer).setScale(2, RoundingMode.UP);
            String s1 = s.substring(0 , 4);
            String s2 = s.substring(4 , 8);
            String s3 = s2+s1;
            Long ret = Long.parseLong(s3, 16);
            BigDecimal bigDecimal = new BigDecimal(ret);
            BigDecimal multiply = bigDecimal.multiply(new BigDecimal("1"));
            System.out.println(multiply);
            collect1.setTypeName("正累积流量");
            //String rounded = MathUtils.rounded(bg.doubleValue());
            collect1.setValue(multiply.toString());
            collect1.setUnit("立方米");
            collect1.setType(type);
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
        }else if (type.equals("302")){
            //当前设备负累计流量
            CollectInformation collect1 = new CollectInformation();
            String s = hexStr.substring(6, 14);
            String s1 = s.substring(0 , 4);
            String s2 = s.substring(4 , 8);
            String s3 = s2+s1;
            Long ret = Long.parseLong(s3, 16);
            BigDecimal bigDecimal = new BigDecimal(ret);
            BigDecimal multiply = bigDecimal.multiply(new BigDecimal("1"));
            System.out.println(multiply);
            collect1.setTypeName("负累积流量");
            //String rounded = MathUtils.rounded(bg.doubleValue());
            collect1.setValue(multiply.toString());
            collect1.setUnit("立方米");
            collect1.setType(type);
            collectData.add(collect1);
            mapData.put("CollectInformation" , collectData);
        }
        return mapData;
    }

    public static void main(String[] args) {
        String hexStr = "0103041FDE0003DDDC";
        String s = hexStr.substring(6, 14);
        String s1 = s.substring(0 , 4);
        String s2 = s.substring(4 , 8);
        String s3 = s2+s1;
        Long ret = Long.parseLong(s3, 16);
        BigDecimal bigDecimal = new BigDecimal(ret);
        BigDecimal multiply = bigDecimal.multiply(new BigDecimal("1"));
        System.out.println(multiply);
    }
}
